<!DOCTYPE html>
<html th:lang="${language}"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('产品项目组列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">

<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-grid"></i> 项目列表
            </div>
            <div class="box-tools pull-right">

                <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i class="fa fa-chevron-up"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i class="fa fa-chevron-down"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i class="fa fa-refresh"></i></button>
            </div>
        </div>
        <div class="ui-layout-content">
            <div class="row col-xs-12" >
                <select id="projectSelect"  onchange="changeProject(this)">
                </select>
            </div>
            <div class="row col-xs-12">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
</div>

<div class="ui-layout-center">

     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden"  id="projectCode" name="projectCode"  />
                    <input type="hidden"  id="groupId" name="groupId"  />
                    <input type="hidden" name="cycleId"   id="cycleId"/>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-info" id="expandAllBtn">
                    <i class="fa fa-exchange"></i> 展开/折叠
                </a>
                <div class="btn-group">
                    <select class="form-control" data-style="btn-success"  id="cycleIdSelect"  th:onchange="changeCycleType()">
                        <optgroup th:label="#{okr.yearChoice}">
                            <div th:each="cycle : ${cycleList}">
                                <div th:if="${cycle.cycleType == 1}">
                                <span th:if="${cycle.status == 0}">
                                        <option th:value="${cycle.id}" selected="selected" th:text="${cycle.cycleName}"></option>
                                </span>
                                    <span th:if="${cycle.status == 1}">
                                        <option th:value="${cycle.id}" th:text="${cycle.cycleName}"></option>
                                </span>
                                </div>
                            </div>
                        </optgroup>
                        <optgroup th:label="#{okr.quarterChoice}">
                            <div th:each="cycle : ${cycleList}">
                                <div th:if="${cycle.cycleType == 0}">
                                <span th:if="${cycle.status == 0}">
                                        <option th:value="${cycle.id}" selected="selected" th:text="${cycle.cycleName}"></option>
                                </span>
                                    <span th:if="${cycle.status == 1}">
                                        <option th:value="${cycle.id}" th:text="${cycle.cycleName}"></option>
                                </span>
                                </div>
                            </div>
                        </optgroup>
                    </select>
                </div>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-tree-table"></table>
            </div>
        </div>
    </div>
</div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">

        $(function() {
            var panehHidden = false;
            if ($(this).width() < 769) {
                panehHidden = true;
            }
            $('body').layout({ initClosed: panehHidden, west__size: 185 });
            queryProjectList();
        });



        var addFlag = [[${@permission.hasPermi('system:group:add')}]];
        var editFlag = [[${@permission.hasPermi('system:group:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:group:remove')}]];
        var projectListFlag = [[${@permission.hasPermi('system:project:projectList')}]];

        var rowTypeDatas = [[${@dict.getType('okr_row_type')}]];
        var scoreStarDatas = [[${@dict.getType('ork_fraction')}]];
        var statusDatas = [[${@dict.getType('row_status')}]];
        var priorityDatas = [[${@dict.getType('okr_level')}]];
        var prefix = ctx + "system/group";

        // /**
        //  * 打开创建界面
        //  * */
        // function createForProject(){
        //     $.operate.add($("#projectCode").val())
        // }

        /**
         * 查询项目列表清单
         */
        function queryProjectList()
        {
            var url = ctx + "system/project/projectList";
            $.ajax({
                url: url,
                data: {},
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                success: function (result) {
                    var len = result.length;
                    if(len>0)
                    {
                        var html = "";
                        for(var i=0;i<len;i++){
                            html+="<option value="+result[i].code+">"+result[i].name+"</option>";
                        }
                        $("#projectSelect").html(html);
                        //初始化查询项目组织结构
                        queryGroupTree(result[0].code);
                        //初始化查询项目组织人员列表
                        $("#projectCode").val(result[0].code);
                        $.table.search();
                    }
                }
            });
        }


        function queryGroupTree(projectCode)
        {
            console.log(projectCode);
            var url = ctx + "system/group/treeData/"+ projectCode;
            var options = {
                url: url,
                expandLevel: 2,
                onClick : zOnClick
            };
            $.tree.init(options);

            function zOnClick(event, treeId, treeNode) {
                $("#groupId").val(treeNode.id);
                $("#projectCode").val($("#projectCode").val());
                $.treeTable.search();
            }
        }

        $('#btnExpand').click(function() {
            $._tree.expandAll(true);
            $(this).hide();
            $('#btnCollapse').show();
        });

        $('#btnCollapse').click(function() {
            $._tree.expandAll(false);
            $(this).hide();
            $('#btnExpand').show();
        });

        $('#btnRefresh').click(function() {
            queryGroupTree();
        });


        $(function() {
            var options = {
                code: "rowId",
                parentCode: "parentId",
                uniqueId: "id",
                expandAll: true,
                expandFirst: false,
                url: prefix + "/okrList",
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove/{id}",
                queryParams: queryParams,
                search:true,
                rowStyle:function(row,index){
                    alert(row+"___"+index);
                },
                modalName: "OKR信息",
                columns: [{
                    field: 'selectItem',
                    radio: true
                },

                    {
                        field: 'rowCode',
                        width: '10%',
                        title: '编号',
                        formatter: function(value, row, index) {
                            var rowNo = '';
                            if(row.rowType==0){
                                rowNo = '<span>O'+value+'</span>';
                            }
                            else {
                                rowNo = '<span>KR'+value+'</span>';
                            }
                            return rowNo;
                        }
                    },
                    {
                        title: 'OKR描述',
                        field: 'content',
                        width: '50%',
                        formatter: function (value, row, index) {
                            if (row.rowType == 0) {
                                return '<H4>' + value + '</H4>';
                            } else {
                                return '<p>' + value + '</p>';
                            }
                        }
                    },
                    {
                        field: 'userName',
                        width: '15%',
                        title: '用户'
                    },
                    // {
                    //     field: 'loginName',
                    //     width: '10%',
                    //     title: '登录名'
                    // },
                    {
                        field: 'deptName',
                        width: '15%',
                        title: '部门'
                    },
                    // {
                    //     field: 'rowType',
                    //     title: '类型',
                    //     width: '5%',
                    //     formatter: function(value, row, index) {
                    //         return $.table.selectDictLabel(rowTypeDatas, value);
                    //     }
                    // },
                    // {
                    //     field: 'confidenceStar',
                    //     title: '信心指数',
                    //     width: '5%',
                    //     formatter: function(value, row, index) {
                    //         return  '<span class="badge badge-success">'+value+'</span>';
                    //     }
                    // },
                    // {
                    //     field: 'scoreStar',
                    //     title: '目标评分',
                    //     width: '5%',
                    //     formatter: function(value, row, index) {
                    //         return $.table.selectDictLabel(scoreStarDatas, value);
                    //     }
                    // },
                    // {
                    //     field: 'process',
                    //     title: '目标进度',
                    //     width: '5%',
                    //     formatter: function(value, row, index) {
                    //         return  '<span class="badge badge-success">'+value+'</span>';
                    //     }
                    // },
                    {
                        field: 'priority',
                        title: '优先级',
                        width: '10%',
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(priorityDatas, value);
                        }
                    }]
            };
            $.treeTable.init(options);


            function queryParams(params) {
                var search = $.table.queryParams(params);
                search.userName = $("#userName").val();
                return search;
            }


        });

        /**
         * 切换项目进行列表数据变更
         * @param event
         */
        function changeProject(event)
        {
            //切换项目的同时 进行组织结构数据数据进行清空
            $("#groupId").val("");
            //重新查询项目组织结构树
            queryGroupTree($(event).val());
            //
            $("#projectCode").val($(event).val());
            //刷新列表数据
            $.treeTable.search();
        }

        /* 分配用户-选择用户 */
        function selectUser() {
            var url =  ctx + "system/group/selectUser/"+ $("#groupId").val();
            $.modal.open("选择用户", url);
        }


        //OKR 周期切换
        function changeCycleType(type) {
            var cycleId = $("#cycleIdSelect").val();
            $("#cycleId").val(cycleId);
            $.treeTable.search();
        }
    </script>
</body>
</html>